package first.endtoend.tsm.webservices;

import first.endtoend.tsm.TSMHelper;
import first.endtoend.tsm.TSMHelperTools;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.GpFunctions;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.IServiceEvents;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.elements.OperationResult;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.ServiceQualifierType;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.requests.CheckMobileSubscriptionEligibilityRequestType;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.requests.data.CheckGlobalEligibilityResponseDataType;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.requests.data.CheckMobileSubscriptionEligibilityResponseDataType;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.requests.responses.CheckGlobalEligibilityResponseType;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.requests.responses.CheckMobileSubscriptionEligibilityResponseType;

import java.math.BigInteger;
import java.util.HashMap;

/**
 * Created by ncordina on 19/06/2015.
 */
public class MobileSubscription extends TSMHelper implements IServiceEvents
{
    private CheckMobileSubscriptionEligibilityResponseType checkMobileSubscriptionEligibilityResponseType = new CheckMobileSubscriptionEligibilityResponseType();
    private HashMap<String, String> display = new HashMap<String, String>();

    /**
     * Class Constructor
     * Take the URL of SPTSM Server in parameter and instanciate the CallBack and the controller of Wolf library
     *
     * @param URL
     */
    public MobileSubscription(String URL)
    {
        super(URL);
    }

    @Override
    public void Starting()
    {
        System.out.println("STARTING MOBILE SUBSCRIPTION");

    }

    @Override
    public synchronized void Completed(OperationResult operationResult)
    {
        if (operationResult.MethodName.equalsIgnoreCase(GpFunctions.CheckMobileSubscriptionEligibility.getFunction()))
        {
            this.getCheckMobileSubscriptionEligibilityResponse(operationResult);
            notify();
        }
    }

    public synchronized HashMap<String, String> checkMobileSubscriptionEligibility() throws InterruptedException
    {
        CheckMobileSubscriptionEligibilityRequestType checkMobileSubscriptionEligibilityRequestType = new CheckMobileSubscriptionEligibilityRequestType();
        checkMobileSubscriptionEligibilityRequestType.FunctionCallIdentifier = GpFunctions.CheckMobileSubscriptionEligibility.getFunction();
        checkMobileSubscriptionEligibilityRequestType.MobileSubscription = tools.initMobileSubcription();
        checkMobileSubscriptionEligibilityRequestType.Service = tools.initService();
        checkMobileSubscriptionEligibilityRequestType.ServiceQualifier = tools.initServiceQualifier();

        tsmController.CheckMobileSubscriptionEligibility(this, checkMobileSubscriptionEligibilityRequestType);
        wait();
        return display;
    }

    /**
     * Call the webservice, take a request object in parameter
     *
     * @param checkMobileSubscriptionEligibilityRequestType
     * @return The request to send to the TSM
     * @throws InterruptedException
     */
    public synchronized HashMap<String, String> checkMobileSubscriptionEligibility(CheckMobileSubscriptionEligibilityRequestType checkMobileSubscriptionEligibilityRequestType) throws InterruptedException
    {
        tsmController.CheckMobileSubscriptionEligibility(this, checkMobileSubscriptionEligibilityRequestType);
        wait();
        return display;
    }

    public synchronized HashMap<String, String> checkMobileSubscriptionEligibility(String mnoName, String somSerialNumber, String serviceID, String versionInfo, ServiceQualifierType serviceQualifier) throws InterruptedException
    {
        CheckMobileSubscriptionEligibilityRequestType checkMobileSubscriptionEligibilityRequestType = new CheckMobileSubscriptionEligibilityRequestType();
        checkMobileSubscriptionEligibilityRequestType.ValidityPeriod = new BigInteger("60000");
        checkMobileSubscriptionEligibilityRequestType.FunctionCallIdentifier = GpFunctions.CheckMobileSubscriptionEligibility.getFunction();
        checkMobileSubscriptionEligibilityRequestType.ServiceQualifier = serviceQualifier;
        checkMobileSubscriptionEligibilityRequestType.MobileSubscription = tools.initMobileSubcription(mnoName);
        checkMobileSubscriptionEligibilityRequestType.Service = tools.initService(serviceID);
        tsmController.CheckMobileSubscriptionEligibility(this,checkMobileSubscriptionEligibilityRequestType);
        wait();
        return display;
    }

    private synchronized void getCheckMobileSubscriptionEligibilityResponse(OperationResult operationResult)
    {
        display = TSMHelperTools.handleResponse(operationResult, GpFunctions.CheckMobileSubscriptionEligibility.getFunction());
        CheckMobileSubscriptionEligibilityResponseType responseType = (CheckMobileSubscriptionEligibilityResponseType) operationResult.Result;
        CheckMobileSubscriptionEligibilityResponseDataType responseDataType = responseType.ResponseData;
        display.put("Eligible",responseDataType.Eligible.toString());
        if(responseDataType.Eligible == false)
        {
            display.put("Non Eligibility Reason", responseDataType.NonEligibilityReason.toString());
        }
    }

    public synchronized HashMap<String, String> getDisplay()
    {
        return display;
    }
}
